package com.kwedding.service;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import com.kwedding.util.LoggerParent;
import com.kwedding.util.Util;
import org.springframework.stereotype.Service;

/**
 * @author Mickey
 * 
 *         2015年3月26日下午5:37:03
 */
@Service
public class SendMail extends LoggerParent {

	/**
	 * 失败信息
	 * 
	 * @param mail
	 * @param code
	 * @return
	 */
	public boolean sendErrorMessage(String title, String message) {
		String body = "<font size = '5'>" + Util.dateToString(new Date())
				+ "<br>错误信息：</font><br><p><font style='color: #DC143C' size='5'>" + message + "</font></p>";
		return send("351947324@qq.com", title, body, null, null, null);
	}

	/**
	 * @param mail
	 *            地址
	 * @param title
	 *            文本
	 * @param body
	 *            内容
	 * @param filePath
	 *            文件地址
	 * @param filename
	 *            文件名称
	 * @param fileDescription
	 *            文件描述
	 * @throws EmailException
	 *             发视异常
	 */
	public boolean send(String mail, String title, String body, String filePath, String filename,
			String fileDescription) {
		try {
			HtmlEmail email = new HtmlEmail();

			email.setHostName("smtp.exmail.qq.com");// smtp.exmail.qq.com
			email.setSmtpPort(465);
			email.setAuthenticator(new DefaultAuthenticator("mike.ma@kweddingapp.com", "Mr.Msn123"));
			email.setSSLOnConnect(true);
			email.setFrom("mike.ma@kweddingapp.com");
			if (filePath != null && !filePath.trim().equals("")) {
				EmailAttachment attachment = new EmailAttachment();
				attachment.setPath(filePath);
				attachment.setDisposition(EmailAttachment.ATTACHMENT);
				attachment.setDescription(fileDescription);
				attachment.setName(filename);// 附件在邮件中显示的名字，中文的乱码问题没有解决，导致附件名有中文时显示乱码
				email.attach(attachment);
			}
			email.setSubject(title);
			email.setHtmlMsg(body);
			email.setContent(body, "text/html;charset=gbk");
			email.addTo(mail);
			email.send();
		} catch (EmailException e) {
			// e.printStackTrace();\

			log.info("---email error:" + e.getMessage());
			return false;
		}
		return true;
	}

	/**
	 * 异常发送邮件
	 * 
	 * @param e
	 */
	public void sendException(Exception e) {
		StringBuffer error = new StringBuffer(e.toString());
		StackTraceElement[] trace = e.getStackTrace();
		for (StackTraceElement traceElement : trace)
			error.append("<br>&emsp;&emsp;at " + traceElement);
		sendErrorMessage("Hospital", error.toString());
	}

	/**
	 * 异常发送邮件
	 * 
	 * @param e
	 */
	public void sendException(HttpServletRequest request, Exception e) {
		StringBuffer error = new StringBuffer();
		if (request != null) {
			error.append("<br>客户端发出请求时的完整URL: " + request.getRequestURL());
			error.append("<br>请求行中的参数部分: " + request.getQueryString());
			error.append("<br>请求的客户机的IP地址: " + request.getRemoteAddr());
			error.append("<br>请求的客户机的完整主机名: " + request.getRemoteHost());
			error.append("<br>服务器的IP地址: " + request.getLocalAddr());
			error.append("<br>服务器的主机名: " + request.getLocalName());
			error.append("<br>请求方式: " + request.getMethod());
		}
		error.append(e.toString());

		StackTraceElement[] trace = e.getStackTrace();
		for (StackTraceElement traceElement : trace)
			error.append("<br>&emsp;&emsp;at " + traceElement);
		sendErrorMessage("Hospital", error.toString());
	}
}